//
//  news.m
//  ios_news
//
//  Created by Han on 14-6-11.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "Activity.h"

@implementation Activity


- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.url  = [attributes valueForKeyPath:@"url"];
    self.imgUrl = [attributes valueForKeyPath:@"img_url"];
    
    //self.user = [[User alloc] initWithAttributes:[attributes valueForKeyPath:@"user"]];
    
    return self;
}


+ (NSURLSessionDataTask *)getDataWithBlock:(void (^)(NSArray *posts, NSError *error))block :(NSString *) url {
    
    AFHTTPSessionManager *sharedClient = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
    sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    
    return [sharedClient GET:@"" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        
        NSArray *responseData = JSON;  // [JSON valueForKeyPath:@"data"]
        
        NSMutableArray *mutableAct = [NSMutableArray arrayWithCapacity:[responseData count]];
        for (NSDictionary *attributes in responseData) {
            Activity *act = [[Activity alloc] initWithAttributes:attributes];
            [mutableAct addObject:act];
        }
        
        if (block) {
            block([NSArray arrayWithArray:mutableAct], nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}

@end
